package com.douyang.base.model;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import lombok.ToString;

import java.io.Serializable;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Mr.M
 * @version 1.0
 * @description 分页查询结果模型类
 * @date 2023/2/11 15:40
 */
@Data
@ToString
public class PageResult<T> implements Serializable {

    // 数据列表
    private List<T> items;

    //总记录数
    private long counts;

    //当前页码
    private long page;

    //每页记录数
    private long pageSize;


    public PageResult(List<T> items, long counts, long page, long pageSize) {
        this.items = items;
        this.counts = counts;
        this.page = page;
        this.pageSize = pageSize;
    }

    public PageResult() {
    }

    public  static <PO,VO> PageResult<VO> of(Page<PO> page, Function<PO,VO> convertor){
        PageResult<VO>pageResult=new PageResult<>();
        pageResult.setPage(page.getPages());
        pageResult.setCounts(page.getTotal());
        pageResult.setPageSize(page.getSize());
        pageResult.setItems(page.getRecords().stream().map(convertor).collect(Collectors.toList()));
        return  pageResult;
    }


}
